package com.lancoo.znbkxx.teacher.aibkTestCoach.checkKnowledgePoint

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.klgcourseware.manager.KlgCourseWareManager
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import kotlinx.android.synthetic.main.aibk_activity_knowledge_list.*
import kotlinx.android.synthetic.main.aibk_dialog_evaluation_export.*
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.util.*

class AibkKnowledgeListActivity : KBaseActivity(), View.OnClickListener {

    companion object {
        fun launch(activity: Activity, listCode: ArrayList<String>, listName:ArrayList<String>, listUniqueCode:ArrayList<String>) {
            val intent = Intent(activity, AibkKnowledgeListActivity::class.java)
            intent.putStringArrayListExtra("ListCode", listCode)
            intent.putStringArrayListExtra("ListName", listName)
            intent.putStringArrayListExtra("ListUniqueCode", listUniqueCode)
            activity.startActivity(intent)
        }
    }

    private val mViewModel: MarkViewModel by viewModel()

    override fun setLayout(): Int {
        return  R.layout.aibk_activity_knowledge_list
    }

    override fun initView() {
        SBtnBack.setOnClickListener(this)
    }

    override fun initData() {
        val listCode = intent.getStringArrayListExtra("ListCode")
        val listName = intent.getStringArrayListExtra("ListName")
        val listUniqueCode = intent.getStringArrayListExtra("ListUniqueCode")

        showKnowledgeListView(this,listCode,listName,listUniqueCode)
    }

    private fun showKnowledgeListView(
            activity:AppCompatActivity,
            listCode:ArrayList<String>,
            listName:ArrayList<String>,
            listUniqueCode:ArrayList<String>
    ){
        val listNameSize = listName.size
        if(listNameSize>0){
            RvKnowledgePointList.layoutManager = LinearLayoutManager(this)
            val adapter = AibkKnowledgePointItem(this,listName)
            adapter.setClickListener(object : AibkKnowledgePointItem.OnClickListener {
                override fun onClick(position:Int) {
//                    val code = listCode[position]
//                    KlgCourseWareManager.getInstance().launchCourseWareKlg(
//                            getHolderActivity(),
//                            code,
//                            Ext.KNOWLEDGEPOINT_URL_COMMMENT,
//                            Ext.CoursewareOBSInfo!!.ServerWithBucket + Ext.CoursewareOBSInfo!!.SysOBSRelativePath,
//                            false, CurrentUser.StudyLevel
//                    )

                    val uniqueCode = listUniqueCode[position]
                    mViewModel.getKlgCode(uniqueCode)
                            .doOnSubscribe {
                                startLoad()
                            }.bindLifeCycle(activity)
                            .subscribe({
                                stopLoad()
                                if (it.code == 1) {
                                    if(Ext.GLOBAL_GRADE == "K6"||Ext.GLOBAL_GRADE == "K7"||Ext.GLOBAL_GRADE == "K8"||Ext.GLOBAL_GRADE == "K9"){
                                        Ext.STAGE_NO = "B"
                                    }else if(Ext.GLOBAL_GRADE == "K10"||Ext.GLOBAL_GRADE == "K11"||Ext.GLOBAL_GRADE == "K12"){
                                        Ext.STAGE_NO = "C"
                                    }
                                    KlgCourseWareManager.getInstance().launchCourseWareKlg(
                                            getHolderActivity(),
                                            it.data.toString(),
                                            Ext.KNOWLEDGEPOINT_URL,
                                            Ext.CoursewareOBSInfo!!.ServerWithBucket + Ext.CoursewareOBSInfo!!.SysOBSRelativePath,
                                            false, Ext.STAGE_NO
                                    )
                                } else {
                                    MyToast.showToast(it.message)
                                }

                            }, {
                                MyToast.showToast(it.message)
                            })

                }
            })
            RvKnowledgePointList.adapter = adapter
        }else{
            RvKnowledgePointList.layoutManager = LinearLayoutManager(this)
            val adapter = AibkAdapterEmptyView(this,"暂无知识点")
            RvKnowledgePointList.adapter = adapter
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.SBtnBack ->{
                finish()
            }
        }
    }

}